package com.usian.service;

import com.usian.mapper.TbContentCategoryMapper;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author lpf
 * @Date 2022/3/24 下午12:58
 * @ProjectName java134-e-commerce-project
 * @Version 1.0
 * @Description:
 */
@Service
@Transactional
public class ContentCategoryServiceImpl implements ContentCategoryService {
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;


    /**
     * 内容匪类管理查询操作
     *
     * @param id
     * @return
     * @Auth lpf
     */
    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(long id) {
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = tbContentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(id);

        return tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
    }

    /**
     * 内容分类管理添加操作
     *
     * @param tbContentCategory
     * @return
     * @Auth lpf
     */
    @Override
    public Integer insertContentCategory(TbContentCategory tbContentCategory) {
        tbContentCategory.setStatus(1);
        tbContentCategory.setIsParent(false);
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setUpdated(new Date());
        int insert = tbContentCategoryMapper.insertSelective(tbContentCategory);

        TbContentCategory tbContentCategoryParent = tbContentCategoryMapper.selectByPrimaryKey(tbContentCategory.getParentId());
        if (!tbContentCategoryParent.getIsParent()) {
            tbContentCategoryParent.setIsParent(true);
            tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategoryParent);
        }
        return insert;
    }


    /**
     * 根据id删除内容管理
     *
     * @param categoryId
     * @return
     * @Auth fgg
     */
    @Override
    public Integer deleteContentCategoryById(Long categoryId) {
        //父节点不可删除
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(categoryId);
        if (tbContentCategory != null && tbContentCategory.getIsParent()) {
            return 0;
        }
        Integer delete = tbContentCategoryMapper.deleteByPrimaryKey(categoryId);

        //如果他不是父节点，就要把他的父节点改为不是父节点
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = tbContentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(tbContentCategory.getParentId());
        List<TbContentCategory> tbContentCategoryList = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        if (tbContentCategoryList == null || tbContentCategoryList.size() == 0) {
            TbContentCategory contentCategoryParent = tbContentCategoryMapper.selectByPrimaryKey(tbContentCategory.getParentId());
            contentCategoryParent.setIsParent(false);
            tbContentCategoryMapper.updateByPrimaryKeySelective(contentCategoryParent);
        }
        return delete;
    }
}
